﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Xml.Serialization.GeneratedAssembly;

namespace ColladaDotNet.ContentPipeline
{
	class Program
	{
		static void Main(string[] args)
		{
			COLLADA.Serializer = new COLLADASerializer();
#if DEBUG
			if (args.Length == 0)
				args = new string[]{"character.py"};
#endif
			if (args.Length > 0)
			{
				if (File.Exists(args[0]))
				{
					switch (Path.GetExtension(args[0]).ToLower())
					{
						case ".js":
							ProcessJScript(args);
							break;
						case ".py":
							ProcessPython(args);
							break;
						case ".dll":
							break;
					} 
				}
			}
		}

		private static void ProcessPython(string[] args)
		{



			var runtime = ScriptRuntime.CreateFromConfiguration();
			ScriptEngine pyEng = runtime.GetEngine("py");

			var testSrc = File.ReadAllText(args[0]);
			ScriptSource source = pyEng.CreateScriptSourceFromString(testSrc, SourceCodeKind.Statements);

			ScriptScope scope = pyEng.CreateScope();
			//try
			//{
			PipelineContext value = new PipelineContext();
			scope.SetVariable("pipeline", value);
				source.Execute(scope);
			//}
			//catch (Exception ex)
			//{
			//    Console.WriteLine(ex.Message);
			//}
		}

		private static void ProcessJScript(string[] args)
		{
			JScriptEvaluator evaluator = (new JScriptEvaluator());
			var text = File.ReadAllText(args[0]);
			PipelineContext context = new PipelineContext();
			evaluator.EvalMethodString(context, text, "pipeline", context);
		}
	}
}
